from models.dept import Dept
from models.user import User
from packages.engine.assignment_handler import AssignmentHandler
from packages.engine.core import Execution
from packages.engine.model.task_model import TaskModel
from sqlalchemy.orm import Session


class ApplicantDeptLeaderAssignmentHandler(AssignmentHandler):
    __sort__ = 10
    __message__ = "发起人所属部门经理"
    __code__ = "services.flow.assignment_handler::ApplicantDeptLeaderAssignmentHandler"

    def assign(self, taskModel: TaskModel, execution: Execution) -> list:
        """
        发起人所属部门经理
        :param taskModel:
        :param execution:
        :return:
        """
        ids = []
        operator = execution.processInstance.operator
        session: Session = execution.engine.db.session
        user: User = session.query(User).filter(User.id == operator).first()
        if user is not None:
            deptId = user.deptId
            dept: Dept = session.query(Dept).filter(Dept.id == deptId).first()
            if dept is not None:
                leaderIds = dept.leaderIds
                if leaderIds is not None and len(leaderIds) > 0:
                    for leaderId in leaderIds.split(","):
                        ids.append(leaderId)
        return ids


class ApplicantDeptMainLeaderAssignmentHandler(AssignmentHandler):
    __sort__ = 20
    __message__ = "发起人所属部门分管领导"
    __code__ = (
        "services.flow.assignment_handler::ApplicantDeptMainLeaderAssignmentHandler"
    )

    def assign(self, taskModel: TaskModel, execution: Execution) -> list:
        """
        发起人所属部门分管领导
        :param taskModel:
        :param execution:
        :return:
        """
        ids = []
        operator = execution.processInstance.operator
        session: Session = execution.engine.db.session
        user: User = session.query(User).filter(User.id == operator).first()
        if user is not None:
            deptId = user.deptId
            dept: Dept = session.query(Dept).filter(Dept.id == deptId).first()
            if dept is not None:
                mainLeaderId = dept.mainLeaderId
                if mainLeaderId is not None:
                    ids.append(mainLeaderId)

        return ids


class DeptLeaderAssignmentHandler(AssignmentHandler):
    __sort__ = 30
    __message__ = "当前用户所属部门经理"
    __code__ = "services.flow.assignment_handler::DeptLeaderAssignmentHandler"

    def assign(self, taskModel: TaskModel, execution: Execution) -> list:
        ids = []
        session: Session = execution.engine.db.session
        user: User = (
            session.query(User)
            .filter(User.id == execution.engine.get_current_user_id())
            .first()
        )
        if user is not None:
            deptId = user.deptId
            dept: Dept = session.query(Dept).filter(Dept.id == deptId).first()
            if dept is not None:
                leaderIds = dept.leaderIds
                if leaderIds is not None and len(leaderIds) > 0:
                    for leaderId in leaderIds.split(","):
                        ids.append(leaderId)
        return ids


class DeptMainLeaderAssignmentHandler(AssignmentHandler):
    __sort__ = 40
    __message__ = "当前用户所属部门分管领导"
    __code__ = "services.flow.assignment_handler::DeptMainLeaderAssignmentHandler"

    def assign(self, taskModel: TaskModel, execution: Execution) -> list:
        ids = []
        session: Session = execution.engine.db.session
        user: User = (
            session.query(User)
            .filter(User.id == execution.engine.get_current_user_id())
            .first()
        )
        if user is not None:
            deptId = user.deptId
            dept: Dept = session.query(Dept).filter(Dept.id == deptId).first()
            if dept is not None:
                mainLeaderId = dept.mainLeaderId
                if mainLeaderId is not None:
                    ids.append(mainLeaderId)

        return ids
